﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using Microsoft.Win32;

namespace Mimoza.Common
{
    public class GlobalSettings
    {
#if WindowsCE
        public static string AgentDBFileName = @"\Жесткий Диск\Documents and settings\Mimoza\Agent.sdf";
        public static string AgentDataFileName = @"\Жесткий Диск\Documents and settings\Mimoza\Data";
        public static string LicenceFileName = @"\Windows\macs.dat";
#else
        public static string AgentDBFileName = "./Agent.sdf";
        public static string LicenceFileName = "macs.dat";
#endif

        public const int c_DefaultPingTimeout = 60 * 5; // in secs - 5 mins

        const string DataSubFolderName = "Data";

        static GlobalSettings()
        {   
        }

        static public string DataPath
        {
            get
            {
                FillDefaultDataPath();
                return m_dataPath;
            }
            set
            {
                SetDataPath(value);
            }
        }

        static public string DesktopPath
        {
            get
            {
#if WindowsCE   
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\Explorer\\Shell Folders", true))
                {
                    if (key != null)
                    {
                        return key.GetValue("Desktop", "").ToString();
                    }
                }

                return "Windows\\Desktop";
#else
                return Environment.GetFolderPath(
                         System.Environment.SpecialFolder.DesktopDirectory);
#endif
            }
        }

        static public string StartupPath
        {
            get
            {
#if WindowsCE
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\Explorer\\Shell Folders", true))
                {
                    if (key != null)
                    {
                        return key.GetValue("StartUp", "").ToString();
                    }
                }

                return "Windows\\StartUp";
#else
                return Environment.GetFolderPath(
                         System.Environment.SpecialFolder.Startup);
#endif
            }
        }

        static public LicenceFile Licence
        {
            get
            {
                return m_licenceFile;
            }
        }

        static public void InitLicenceFile()
        {
            m_licenceFile = new LicenceFile();
            m_licenceFile.Init(Path.Combine(DataPath, LicenceFileName));
        }

        static void FillDefaultDataPath()
        {
            lock (m_dataPath)
            {
#if WindowsCE
                m_dataPath = AgentDataFileName;
#else
                if (string.IsNullOrEmpty(m_dataPath))
                {
                    string path = Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName;
                    path = Path.GetDirectoryName(path);
                    m_dataPath = Path.Combine(path,DataSubFolderName);
                }
#endif
            }
        }

        static void SetDataPath(string dataPath)
        {
            lock (m_dataPath)
            {
                m_dataPath = dataPath;
            }
        }

        static string m_dataPath = "";
        static LicenceFile m_licenceFile = new LicenceFile();
    }
}
